/**
 * @author Xiao Xin
 * @version 1.0.0
 * @date 20210409
 * @copyright MIN-Group:国家重大科技基础设施-未来网络北大实验室：深圳市信息论与未来网络重点实验室
 */
 var SelfEncodingBase = require('../encoding/SelfEncodingBase').SelfEncodingBase
 var Encoder = require('../encoding/Encoder').Encoder
 var Block = require('../encoding/Block').Block
 var TLV = require('../encoding/TLV')
 var {TlvComponentBase,InitialAble} = require('./TlvComponentBase')
 var aggregation = require('../common/aggregation')//实现了多继承

//
// 表示一个控制命令参数 Capacity
//
// @Description:
//
class ControlParameterCapacity extends aggregation(TlvComponentBase,InitialAble,SelfEncodingBase){
  constructor(){
    super()
    this.capacity = BigInt(0)
  }

  getCapacity(){
    return this.capacity;
  }

  setCapacity(capacity){
    this.capacity = BigInt(capacity)
    this.doInitial()
  }

  // 将 ControlParameterCapacity 线速编码为一个TLV
  //
  // @Description:
  // @receiver c
  // @param encoder{Encoder}
  // @return int
  // @return error
  //
  wireEncode(encoder){
    var totalLength = 0
    try {
    //编码 TLV-VALUE
    var tmpLen = encoder.prependNonNegativeInteger(this.capacity)
    totalLength += tmpLen 

    // 编码 TLV-LENGTH
    tmpLen = encoder.prependVarNumber(BigInt(totalLength))
    totalLength += tmpLen

    // 编码 TLV-TYPE
    tmpLen = encoder.prependVarNumber(TLV.ComponentType.TlvManagementCapacity)
    totalLength += tmpLen

    return totalLength
    } catch (error) {
      throw error
    }
  }

  //
  // 从 TLV Block 中解码出一个 ControlParameterCapacity
  //
  // @Description:
  // @receiver c
  // @param block{Block}
  // @return error
  //
  wireDecode(block){
    try {
        // 检查 Type 是否正确
        if(TLV.expectType(block.getType(), TLV.ComponentType.TlvManagementCapacity)){
          var value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength())
        }
        this.setCapacity(value)
    } catch (error) {
      throw error
    }
  }
}

module.exports = {
  ControlParameterCapacity:ControlParameterCapacity
}